﻿// ===============================================================================
// Project Name        :	Weisenz.Core
// Project Description :	Weisenz系统核心类库。
// ===============================================================================
// Class Name          :	TemplateHelper
// Class Version       :	v1.0.0.0
// Class Description   :	Velocity 模版引擎操作类。
// Author              :	Charles
// Create Time         :	2012/3/29 
// Update Time         :	2012/3/29 
// ===============================================================================
// Copyright © Weisenz Corporation 2012 . All rights reserved.
// ===============================================================================
using System.IO;
using System.Web;
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;

namespace Weisenz.Core
{
    /// <summary>
    /// Velocity 模版引擎操作类。
    /// </summary>
    public class TemplateEngine
    {
        private VelocityEngine _velocity = null;
        private IContext _context = null;
        //  private string templatFileName;

        /// <summary>
        /// 初始化 TemplateHelper 类的实例。
        /// </summary>
        /// <param name="templatePath">模板文件路径。</param>
        public TemplateEngine(string templatePath)
        {
            _velocity = new VelocityEngine();

            //使用设置初始化VelocityEngine
            ExtendedProperties props = new ExtendedProperties();

            props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatePath);
            props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
            _velocity.Init(props);
            //RuntimeConstants.RESOURCE_MANAGER_CLASS 
            //为模板变量赋值
            _context = new VelocityContext();

        }

        /// <summary>
        /// 给模板变量赋值
        /// </summary>
        /// <param name="key">模板变量</param>
        /// <param name="value">模板变量值</param>
        public void Put(string key, object value)
        {
            _context.Put(key, value);
        }

        /// <summary>
        /// 生成字符
        /// </summary>
        /// <param name="templatFileName">模板文件名</param>
        public string BuildString(string templateFile)
        {
            //从文件中读取模板
            Template template = _velocity.GetTemplate(templateFile);
            //合并模板
            StringWriter writer = new StringWriter();
            template.Merge(_context, writer);
            return writer.ToString();
        }
        /// <summary>
        /// 显示模板
        /// </summary>
        /// <param name="templatFile">模板文件名</param>
        public void Display(string templatFile)
        {
            //从文件中读取模板
            Template template = _velocity.GetTemplate(templatFile);
            //合并模板
            StringWriter writer = new StringWriter();
            template.Merge(_context, writer);
            //输出
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Write(writer.ToString());
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
        }
    }
}
